[][src]Crate ocaml

ocaml-rs is a library for directly interacting with the C OCaml runtime, in Rust.

Note: ocaml-rs is still experimental, please report any issues on github

This crate will allow you to write OCaml C stubs directly in Rust. It is suggested to build a static library to link to when compiling your OCaml code.

The following in Rust:

caml!(ml_add_10(arg) {
    caml_local!(x);
    let n = arg.i32_val();
    x = Value::i32(n + 10);
    return x;
});

is equivalent to:

value ml_add_10(value arg) {
    CAMLparam1(arg);
    CAMLlocal(result);
    int n = Int_val(arg);
    result = Val_int(arg + 10)
    CAMLreturn(result);
}

using the traditional C bindings.

Here are a few more examples...

caml!(build_tuple(i) {
    let i = i.val_i32();
    Tuple::from(&[i + 1, i + 2, i + 3])
});

caml!(average(arr) {
    let arr = Array::from(arr);
    let len = arr.len();
    let sum = 0f64;

    for i in 0..len {
        sum += arr.get_double_unchecked(i);
    }

    Value::f64(sum / len as f64)
});

In OCaml the stubs for these functions looks like this:

external build_tuple: int -> int * int * int = "build_tuple"
external average: float array -> float = "average"

For more examples see ./example or ocaml-vec.

Re-exports

pub use crate::runtime::*;
pub use crate::value::FromValue;
pub use crate::value::ToValue;
pub use crate::value::Value;

Modules

conv
core
runtime
value

Macros

array

Create an OCaml array

caml

Defines an external Rust function for FFI use by an OCaml program, with automatic CAMLparam, CAMLlocal, and CAMLreturn inserted for you.

caml_body

Defines an OCaml FFI body, including any locals, as well as a return if provided; it is up to you to define the parameters.

caml_ffi
caml_frame

Defines an OCaml frame

caml_local

Initializes and registers the given identifier(s) as a local value with the OCaml runtime.

caml_param

Registers OCaml parameters with the GC

list

Create an OCaml list

tuple

Create an OCaml tuple

Structs

Array

OCaml Array type

Array1

OCaml Bigarray.Array1 type

List

OCaml list type

Str

OCaml String type

Tuple

OCaml Tuple type

Enums

Error

Error returned by ocaml-rs functions

Tag

Tags are used to determine the type of value that is stored in an OCaml value

Functions

named_value

Returns a named value registered by OCaml